package com.smooth.site.sx.pc.company.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smooth.common.pc.enums.LoginType;
import com.smooth.common.pc.interceptor.PcAccountContext;
import com.smooth.common.site.common.MD5PawService;
import com.smooth.common.site.common.enums.SiteCodeEnums;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.rsp.ReturnResult;
import com.smooth.dao.gen.bean.BizCompany;
import com.smooth.dao.genx.bean.HotCompRsp;
import com.smooth.site.sx.mag.biz.comp.service.BizCompanyService;
import com.smooth.site.sx.pc.company.req.CompLoginReq;
import com.smooth.site.sx.pc.company.req.CompRegReq;
import com.smooth.site.sx.pc.company.req.HotCompReq;
import com.smooth.site.sx.pc.company.service.PcCompService;
import com.smooth.sys.site.biz.sms.bx.service.SmsCodeService;

@Controller
@RequestMapping(value = "/pc")
public class PcCompanyController {
	@Autowired
	PcCompService pcCompService;
	@Autowired
	SmsCodeService smsCodeService;
	@Autowired
	BizCompanyService bizCompanyService;
	@RequestMapping(value = {"p/comp/register"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult register(@Valid CompRegReq compRegReq, BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		
		BizCompany bizCompany = pcCompService.queryCompByTelNo(compRegReq.getTelNo());
		if(bizCompany!=null) {
			throw new SiteException(SiteCodeEnums.COMP_EXIST,compRegReq.getAccountNo());
		}else {
			bizCompany = pcCompService.registerComp(compRegReq);
			returnResult.setData(bizCompany);
			
			
			returnResult.setData("comp", bizCompany);
			returnResult.setData("token", MD5PawService.getMd5Psw(bizCompany.getContactTelNo()));
			
			PcAccountContext.setObj(bizCompany.getClass(), bizCompany);
			PcAccountContext.setLoginType(LoginType.company);
		}
		return returnResult;
	}
	
	@RequestMapping(value = {"p/comp/login"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult login(@Valid CompLoginReq compLoginReq, BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizCompany bizCompany = pcCompService.queryCompByTelNo(compLoginReq.getTelNo());
		if(bizCompany==null) {
			throw new SiteException(SiteCodeEnums.LOGIN_FAILED,"账户"+compLoginReq.getTelNo()+"不存在，请注册");
		}else {
			if(!smsCodeService.verfyCode(compLoginReq.getTelNo(), compLoginReq.getVerCode())) {
				throw new SiteException(SiteCodeEnums.LOGIN_FAILED,"账户"+compLoginReq.getTelNo()+"验证码错误，请重试");
			}
			returnResult.setData("comp", bizCompany);
			returnResult.setData("token", MD5PawService.getMd5Psw(compLoginReq.getTelNo()));
			
			PcAccountContext.setObj(bizCompany.getClass(), bizCompany);
			PcAccountContext.setLoginType(LoginType.company);
		}
		return returnResult;
	}
	
	
	@RequestMapping(value = {"c/comp/upd"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult upd(@Valid BizCompany bizCompany, BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		
		BizCompany secBizCompany = (BizCompany)PcAccountContext.getObj(BizCompany.class);
		if(secBizCompany==null) {
			throw new SiteException(SiteCodeEnums.AUTH_NOT_ENOUGH,"请登录");
		}
		BizCompany bizCompanyDb = pcCompService.queryCompByAccountNo(secBizCompany.getAccountNo());
		if(bizCompanyDb==null) {
			throw new SiteException(SiteCodeEnums.LOGIN_FAILED,"账户"+secBizCompany.getAccountNo()+"不存在，请注册");
		}else {
			bizCompany.setId(bizCompanyDb.getId());
			returnResult.setData(pcCompService.updComp(bizCompany));
		}
		return returnResult;
	}
	
	
	@RequestMapping(value = {"c/comp/query"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult ccompQuery(HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizCompany bizCompany = (BizCompany)PcAccountContext.getObj(BizCompany.class);
		if(bizCompany==null) {
			throw new SiteException(SiteCodeEnums.AUTH_NOT_ENOUGH,"请登录");
		}else {
			returnResult.setData(bizCompany);
		}
		return returnResult;
	}
	
	@RequestMapping(value = {"p/comp/query"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult upd(@RequestParam(required=true) String compId,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizCompany bizCompany = bizCompanyService.get(compId);
		if(bizCompany==null) {
			throw new SiteException(SiteCodeEnums.param_error,"该公司不存在，请刷新");
		}else {
			returnResult.setData(bizCompany);
		}
		return returnResult;
	}
	
	@RequestMapping(value = {"p/comp/list"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult list(@Valid HotCompReq hotCompReq,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		List<HotCompRsp> rlist = pcCompService.queryHotComp(hotCompReq);
		returnResult.setData(rlist);
		return returnResult;
	}
	
	
	@RequestMapping(value = {"p/comp/hot"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult hotComp(@Valid HotCompReq hotCompReq,BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		List<HotCompRsp> rlist = pcCompService.queryHotComp(hotCompReq);
		returnResult.setData(rlist);
		return returnResult;
	}
	
}
